package com.farina.tracescleaner;

import com.farina.tracescleaner.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	public static final String STARTUP_DATA = "startup_data_file";
	public static Intent controlIntent=null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SharedPreferences settings = getSharedPreferences(MainActivity.STARTUP_DATA, 0);
		boolean isActive = settings.getBoolean("isActive", false);
		ToggleButton cb= (ToggleButton)findViewById(R.id.onOffButton);
		EditText period = (EditText) findViewById(R.id.periodText);
		if(isActive){
			cb.setChecked(true);
			CheckBox call=(CheckBox) findViewById(R.id.callLogCheckbox);
			CheckBox sms=(CheckBox) findViewById(R.id.smsCheckbox);
			CheckBox appData=(CheckBox) findViewById(R.id.appDataCheckbox);
			CheckBox browserData=(CheckBox) findViewById(R.id.browserCheckbox);
			CheckBox whatsappData=(CheckBox) findViewById(R.id.whatsAppCheckbox);

			boolean callSet = settings.getBoolean("callCanc", false);
			boolean smsSet = settings.getBoolean("smsCanc", false);
			boolean appDataSet = settings.getBoolean("appDataCanc", false);
			boolean browserDataSet = settings.getBoolean("browserDataCanc", false);
			boolean whatsappDataSet = settings.getBoolean("whatsappDataCanc", false);

			int periodSet = settings.getInt("ctrlTimePeriod", 10000);
			call.setChecked(callSet);
			sms.setChecked(smsSet);
			appData.setChecked(appDataSet);
			browserData.setChecked(browserDataSet);
			whatsappData.setChecked(whatsappDataSet);
			period.setText((periodSet/1000)+"");
			startControlService();
		}
		else{
			cb.setChecked(false);
			period.setText("10");
		}
		buttonActivationCheck();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	public void startControlService(){
		EditText period = (EditText) findViewById(R.id.periodText);
		CheckBox call=(CheckBox) findViewById(R.id.callLogCheckbox);
		CheckBox sms=(CheckBox) findViewById(R.id.smsCheckbox);
		CheckBox appData=(CheckBox) findViewById(R.id.appDataCheckbox);
		CheckBox browserData=(CheckBox) findViewById(R.id.browserCheckbox);
		CheckBox whatsappData=(CheckBox) findViewById(R.id.whatsAppCheckbox);

		int periodSet=Integer.parseInt(period.getText().toString());
		if(periodSet<1){
			ToggleButton cb= (ToggleButton)findViewById(R.id.onOffButton);
			cb.setChecked(false);
			Toast toast = Toast.makeText(this, "Incorrect Period (<1)", Toast.LENGTH_SHORT);
			toast.show();
		}

		SharedPreferences settings = getSharedPreferences(STARTUP_DATA, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("isActive", true);
		editor.putInt("ctrlTimePeriod", periodSet*1000);
		editor.putBoolean("callCanc", call.isChecked());
		editor.putBoolean("smsCanc", sms.isChecked());
		editor.putBoolean("appDataCanc", appData.isChecked());
		editor.putBoolean("browserDataCanc", browserData.isChecked());
		editor.putBoolean("whatsappDataCanc", whatsappData.isChecked());

		editor.commit();
		MainActivity.controlIntent=new Intent(this, ControlService.class);
		startService(MainActivity.controlIntent);
		ControlService.service=this;


	}

	private void buttonActivationCheck(){
		EditText period = (EditText) findViewById(R.id.periodText);
		CheckBox call=(CheckBox) findViewById(R.id.callLogCheckbox);
		CheckBox sms=(CheckBox) findViewById(R.id.smsCheckbox);
		CheckBox appData=(CheckBox) findViewById(R.id.appDataCheckbox);
		CheckBox browserData=(CheckBox) findViewById(R.id.browserCheckbox);
		CheckBox whatsappData=(CheckBox) findViewById(R.id.whatsAppCheckbox);
		ToggleButton cb= (ToggleButton)findViewById(R.id.onOffButton);

		if(cb.isChecked()){
			period.setEnabled(false);
			call.setEnabled(false);
			sms.setEnabled(false);
			appData.setEnabled(false);
			browserData.setEnabled(false);
			whatsappData.setEnabled(false);
		}
		else{
			period.setEnabled(true);
			call.setEnabled(true);
			sms.setEnabled(true);
			appData.setEnabled(true);
			browserData.setEnabled(true);
			whatsappData.setEnabled(true);
			if(call.isChecked() ||
					sms.isChecked()  ||
					appData.isChecked() ||
					browserData.isChecked() ||
					whatsappData.isChecked()){
				cb.setEnabled(true);
			}
			else cb.setEnabled(false);
		}
	}

	public void checkListener(View v){
		buttonActivationCheck();
	}

	public void onOffButtonListener(View v){

		boolean on = ((ToggleButton) v).isChecked();

		if (on) {
			startControlService();
			buttonActivationCheck();

		} else {
			stopService(MainActivity.controlIntent);
			SharedPreferences settings = getSharedPreferences(STARTUP_DATA, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean("isActive", false);
			editor.putInt("ctrlTimePeriod", 10000);
			editor.commit();
			buttonActivationCheck();
		}

	}

}
